package com.xiaochong.tir.manager.controller;

import java.util.Date;

import javax.servlet.http.HttpSession;

import com.xiaochong.service.common.config.redis.RedisServer;
import com.xiaochong.tir.common.util.RedisKeysUtils;
import com.xiaochong.tir.manager.util.SessionUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.xc.logclient.utils.LogTrace;
import com.xiaochong.tir.common.data.po.SyLabel;
import com.xiaochong.tir.manager.enums.StatusEnum;
import com.xiaochong.tir.manager.service.SyLabelService;
import com.xiaochong.tir.manager.util.ResultEntity;
import com.xiaochong.tir.manager.util.ReturnResult;

/**   
 * @ClassName:  SyLabelController   
 * @Description:TODO(标签管理)   
 * @author: maxinhong
 * @date:   2018年5月7日 下午5:00:38   
 */
@Controller
@RequestMapping("/manage/syLabel")
@Api("标签管理")
public class SyLabelController {

	@Autowired
	private SyLabelService syLabelService;
	@Autowired
	private RedisServer redisServer;

	@RequestMapping(value = "/syLabelList", method = RequestMethod.GET)
	public String syChannelsList() {
		return "/syLabel/syLabelList";
	}
	
	@RequestMapping(value = "/addSyLabel", method = RequestMethod.GET)
	public String addSyChannels() {
		return "/syLabel/syLabelAdd";
	}

	/**   
	 * @Title: listSyLabel   
	 * @Description: TODO(列表)   
	 * @param: @param find
	 * @param: @param page
	 * @param: @param limit
	 * @param: @return      
	 * @return: Object      
	 * @throws   
	 */
	@ApiOperation(value = "查询标签列表",notes = "查询标签列表")
	@RequestMapping(value = "/listSyLabel")
	@ResponseBody
	public Object listSyLabel(@RequestParam(value = "find", required = false) String find,
			@RequestParam("page") int page,@RequestParam("limit") int limit) {
		try {
			PageInfo<SyLabel> pageInfo = syLabelService.getSyLabelList(find,page, limit);
			return new ReturnResult<SyLabel>(0, "查询成功", pageInfo.getList(), pageInfo.getTotal());
		} catch (Exception e) {
			e.printStackTrace();
			LogTrace.error(getClass().getSimpleName() + "/listSyLabel", e.getMessage(),e);
		}
		return null;
	}

	
	/**   
	 * @Title: saveSyLabel   
	 * @Description: TODO(新增)   
	 * @param: @param syLabel
	 * @param: @param session
	 * @param: @param model
	 * @param: @return      
	 * @return: Object      
	 * @throws   
	 */
	@ApiOperation(value = "新增标签",notes = "新增标签")
	@RequestMapping("/saveSyLabel")
	public Object saveSyLabel(SyLabel syLabel) {
		redisServer.set(RedisKeysUtils.LOGINNAME,"新增标签");
		try {
			SyLabel label = syLabelService.selectByEntity(syLabel);
			if(null != label){
				return "/syLabel/syLabelList";
			}
			syLabel.setEnableFlag(syLabel.getStatus().equals(1)?true:false);
			syLabel.setDeleteFlag(false);
			syLabel.setCreateTime(new Date());
			syLabel.setUpdateTime(new Date());
			syLabelService.insertSelective(syLabel);
			return "/syLabel/syLabelList";
		} catch (Exception e) {
			e.printStackTrace();
			LogTrace.error(getClass().getSimpleName() + "/saveSyLabel", e.getMessage(),e);
		}
		return null;
	}
	
	/**   
	 * @Title: updateSyLabelStatus   
	 * @Description: TODO(改状态)   
	 * @param: @param id
	 * @param: @param enableFlag true ：启用； false : 未启用
	 * @param: @return      
	 * @return: ResultEntity<JSONObject>      
	 * @throws   
	 */
	@ApiOperation(value = "新增标签状态",notes = "新增标签状态")
	@GetMapping("/updateSyLabelStatus")
    @ResponseBody
    public ResultEntity<JSONObject> updateSyLabelStatus(@RequestParam("id") Integer id,
														@RequestParam("enableFlag") String enableFlag){
		redisServer.set(RedisKeysUtils.LOGINNAME,"标签启用/停用");
		ResultEntity<JSONObject> result = new ResultEntity<>();
        try {
        	SyLabel syLabel = new SyLabel();
        	syLabel.setId(id);
        	syLabel.setEnableFlag("true".equals(enableFlag)?false:true);
        	syLabelService.updateByPrimaryKey(syLabel,syLabelService.selectByPrimaryKey(id));
            result.setStatus(StatusEnum.OK);
        } catch (Exception e) {
            result.setStatus(StatusEnum.SERVER_ERROR);
            LogTrace.error(getClass().getSimpleName() + "/updateSyLabelStatus", e.getMessage(),e);
        }
        return result;
    }
}
